<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\CouponModel;
use app\model\StaffModel;
use think\facade\Request;
use think\facade\Validate;

class Coupon
{
    //获取优惠卷列表
    public function list()
    {
        // $params = Request::param();
        // $params['dishes_status'] = $params['dishes_status'] ?? 0;
        // $params['dishes_name'] = $params['dishes_name'] ?? '';

        // $validate = Validate::rule([
        //     'dishes_status|菜品状态' => 'require'
        // ]);
        // if (!$validate->check($params)) {
        //     $data = [
        //         'status' => 10002,
        //         'message' => $validate->getError(),
        //         'data' => []
        //     ];
        //     return json($data);
        // }
        // $model = DishesModel::where('dishes_id', '>', 0);
        // if($params['dishes_status']!=0){
        //     $model->where("dishes_status","=",$params['dishes_status']);
        // };
        // if($params['dishes_name']){
        //     $model->where("dishes_name","like",'%'.$params['dishes_name'].'%');
        // };
        $couponList=CouponModel::select();
        foreach ($couponList as &$value){
            //操作员
            $staffOne = StaffModel::find($value['staff_id']);
            $value['username'] = $staffOne['username'] ?? 'null';
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'couponList' => $couponList
            ],
        ];
        return json($data);
    }

    //优惠卷增加
    public function add()
    {
        $params = Request::param();
        
        $validate = Validate::rule([
            'full|满减中的满' => 'require',
            'reduce|满减中的减' => 'require',
            'day|有效的使用天数' => 'require',
            'start_time|生效日期'=> 'require',
            'end_time|有效期限' => 'require',
            'staff_id|员工id' => 'require',
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
//        $params['dishes_status']=1;
        $params['add_time']=$params['update_time']=time();
        $result=CouponModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //优惠卷删除
    public function delete()
    {
        $couponId = Request::param("coupon_id");
        $validate = Validate::rule([
            'coupon_id|优惠券id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['coupon_id' => $couponId])) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $result = CouponModel::destroy($couponId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //优惠卷修改
    public function update()
    {

        $params = Request::param();
        $validate = Validate::rule([
            'coupon_id|优惠券id' => 'require|between:1,' . PHP_INT_MAX,
            'full|满减中的满' => 'require',
            'reduce|满减中的减' => 'require',
            'day|有效的使用天数' => 'require',
            'start_time|生效日期'=> 'require',
            'end_time|有效期限' => 'require',
            'staff_id|员工id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $couponOne = CouponModel::find($params['coupon_id']);

        if ($params['full']){
            $couponOne['full']=$params['full'];
        }
        if ($params['reduce']){
            $couponOne['reduce']=$params['reduce'];
        }
        if ($params['day']){
            $couponOne['day']=$params['day'];
        }
        if ($params['start_time']){
            $couponOne['start_time']=$params['start_time'];
        }
        if ($params['end_time']){
            $couponOne['end_time']=$params['end_time'];
        }
        if ($params['staff_id']){
            $couponOne['staff_id']=$params['staff_id'];
        }
        
        $couponOne['update_time'] = time();
        $result = $couponOne->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
